package com.acknight.service.impl;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.acknight.constant.RedisConstants;
import com.acknight.dto.Result;
import com.acknight.entity.ShopType;
import com.acknight.mapper.ShopTypeMapper;
import com.acknight.service.IShopTypeService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ShopTypeServiceImpl extends ServiceImpl<ShopTypeMapper, ShopType> implements IShopTypeService {

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 查询店铺类型
     * @return
     */
    @Override
    public Result queryTypes() {
//        // 1.查询redis中有没有店铺类型数据
//        String key = RedisConstants.TYPE_SHOP_KEY;
//        Long size = stringRedisTemplate.opsForValue().size(key);
//
//        // 2.有店铺类型数据，直接将缓存取出并返回
//        if (size > 0) {
//            String s = stringRedisTemplate.opsForValue().get(key);
//            List<ShopType> typeList = JSONUtil.toList(s, ShopType.class);
//            return Result.ok(typeList);
//        }
//
//        // 3.没有店铺类型数据，查询数据库，并将数据存入redis，并将数据返回
//        List<ShopType> typeList = query().orderByAsc("sort").list();
//        stringRedisTemplate.opsForValue().append(key, JSONUtil.toJsonStr(typeList));
        LambdaQueryWrapper<ShopType> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShopType::getIsDelete, 0);
        List<ShopType> typeList = list(queryWrapper);
        return Result.ok(typeList);
    }
}
